
#include <iostream>
#include <cassert>
#include <cstring>
using namespace std;

namespace zx
{
	template<class T>
	class vector
	{	 
     public:
         typedef T* iterator;
         typedef const T* const_iterator;
         vector()
			 :_start(nullptr),
			 _finish(nullptr),
			 _endofstorage(nullptr)
		 {

		 }
         
         vector(const vector<T>& x)
			 :_start(nullptr),
			 _finish(nullptr),
			 _endofstorage(nullptr)
		 {
            // T* tmp = new T[x.capacity()];
			// for(size_t i = 0;i<x.size();++i)
			// {
			//	 tmp[i] = x[i];
			// }
			// _start = tmp;
			// _finish = _start + x.size();
			// _endofstorage = _start + x.capacity();
			 vector<T> tmp(x.begin(),x.end());
			 swap(tmp);
		 }
         
		 template<class Inputiterator>
		 vector(Inputiterator first,Inputiterator last)
		     :_start(nullptr),
			 _finish(nullptr),
			 _endofstorage(nullptr)
		 {
             while(first != last)
			 {
				 push_back(*first);
				 ++first;
			 }
		 }

		 ~vector()
		 {
			 delete[] _start;
			 _start = _finish = _endofstorage = nullptr;
		 }

		 vector<T>& operator=(vector<T> x)
		 {
             swap(x);
			 return *this;
		 }

		 void push_back(const T& x)
		 {
            // if(_finish == _endofstorage)
			// {
			//	 reserve(capacity() == 0?4:capacity()*2);
			// }
			// *_finish = x;
			// ++_finish;
			 insert(_finish,x);
		 }

		 void pop_back()
		 {   
			 assert(_finish > _start);
		     --_finish;
		 }

		 void reserve(size_t n)
		 {
			 if(n > capacity())
			 {
                 T*tmp = new T[n];
				 for(size_t i=0;i<size();++i)
				 {
					 tmp[i] = _start[i];
				 }
				 delete[] _start;
                 _finish = size() + tmp;
				 _start = tmp;
				 _endofstorage = _start + n;
			 }
		 }

		 void resize(size_t n,const T& x = T())
		 {
			 if(n > size())
			 {
				 if(n > capacity())
				 {
                     reserve(n);
				 }	 
			     while(_finish != _start + n)
				 {
                     *_finish = x;
					 ++_finish;
				 }
				 
			 }
             else if(n < size())
			 {
				 _finish = _start + n;
			 }
			 else
				 ;
		 }
         
		 iterator insert(iterator pos,const T& x = T())
		 {
			 assert(pos >= _start && pos <= _finish);
             if(_finish == _endofstorage)
			 {
			     size_t n = pos - _start;//解决迭代器失效问题
				 reserve(capacity() == 0?4:capacity()*2);
			     pos = _start + n;
			 }
             iterator end = _finish;
			 while(end - 1 >= pos)
			 {
				 *end = *(end - 1);
				 --end;
			 }
			 *pos = x;
			 ++_finish;
			 return pos;
		 }

		 iterator erase(iterator pos)
		 {
             assert(pos >= _start && pos < _finish);
			 iterator begin = pos + 1;
			 while(begin < _finish)
			 {
				 *(begin - 1) = *begin;
				 ++begin;
			 }
			 --_finish;
			 return pos;
		 }

		 T& operator[](size_t pos)
		 {
			 assert(pos < size());
			 return _start[pos];
		 }
         
         void swap(vector<T>& x)
		 {
			 std::swap(_start,x._start);
			 std::swap(_finish,x._finish);
			 std::swap(_endofstorage,x._endofstorage);
		 }

		 size_t size()
		 {
			 return _finish-_start;
		 }

		 size_t capacity()
		 {
			 return _endofstorage-_start;
		 }

		 iterator begin()
		 {
			 return _start;
		 }

		 iterator end()
		 {
			 return _finish;
		 }

	 private:
		 iterator _start;
         iterator _finish;
		 iterator _endofstorage;
	};
}



int main()
{
    zx::vector<string> v1;
	v1.push_back("zx");
	v1.push_back("yy");
	v1.push_back("tl");
	v1.insert(v1.begin(),"tm");
	zx::vector<string>::iterator it =v1.begin(); 
    v1.insert(++it,"ji");
	v1.insert(v1.end(),"lal");
	for(auto v:v1)
	{
		cout<<v<<" ";
	}
	cout<<endl;
	cout<<v1.size()<<" "<<v1.capacity()<<endl;
	v1.erase(v1.begin());
	v1.erase(v1.end()-1);
	for(auto v:v1)
	{
		cout<<v<<" ";
	}
	cout<<v1.size()<<" "<<v1.capacity()<<endl;
	return 0;
}
